package com.kitty.game.task.model.user;

import com.kitty.game.task.model.product.TaskType;
import lombok.Getter;
import lombok.Setter;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

import java.util.*;
//后加
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
 * 玩家任务数据
 */
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown=true)
public class RoleTask {
    /**不可提交*/
    public static final byte NOT_SUBMIT = 0;
    /**可以提交*/
    public static final byte CAN_SUBMIT = 1;

    /**已经接取任务，key: 任务id  value: 状态*/
    private Map<Integer, Byte> acceptedTasks = new HashMap<Integer, Byte>();

    /**已经完成任务，元素: 任务id*/
    private Set<Integer> finishedTasks = new HashSet<Integer>();

    /**********************/

    /**正在进行的任务的进度*/

    private Map<Integer, DoingTask> doingTask = new HashMap<Integer, DoingTask>();

    /**放弃任务时间*/
    private Map<Integer, Long> giveUpTimes;

    /**弹出采集的任务*/
    private transient int gatherTaskId;


    /**********************/

    /**
     * 拉取任务
     * @param taskId
     */
    public void acceptTask(int taskId) {
        acceptedTasks.put(taskId, NOT_SUBMIT);
        this.doingTask.put(taskId, new DoingTask(taskId, 0));
    }

    /***
     * 标记可提交状态
     */
    public void updateToCanSubmit(int taskId) {
        Byte status = acceptedTasks.get(taskId);
        if (status == null) {return ;}
        if (status == NOT_SUBMIT) {
            acceptedTasks.put(taskId, CAN_SUBMIT);
        }
    }

    /**
     * 把任务标记为已完成状态
     * @param taskId
     */
    public void updateToFinished(int taskId) {
        /**需要把已接取任务中移除，再加入已完成列表*/
        removeAcceptedTask(taskId);
        removeDoingTask(taskId);

        finishedTasks.add(taskId);
    }

    /**
     * 任务是否可以提交
     * @param taskId
     * @return
     */
    public boolean isCanSubmit(int taskId) {
        Byte status = acceptedTasks.get(taskId);
        if (status == null) {return false;}
        if (status != CAN_SUBMIT) {
            return false;
        }
        return true;
    }

    /**任务是否已经接取*/
    public boolean isAccepted(int taskId) {
        return acceptedTasks.containsKey(taskId);
    }

    /**
     * 任务是否完成
     * @param taskId
     * @return
     */
    public boolean isFinished(int taskId) {
        if (finishedTasks.contains(taskId)) {
            return true;
        }
        return false;
    }

    /**
     * 从已接取任务中移除
     * @param taskId
     */
    private void removeAcceptedTask(int taskId) {
        acceptedTasks.remove(taskId);
    }

    /**从正在进行的任务移除*/
    private void removeDoingTask(int taskId) {
        doingTask.remove(taskId);
    }

    /**
     * 获得对应任务的目标进度
     * @param taskId
     * @return
     */
    public int getTargetProgress(int taskId) {
        DoingTask doingTask = this.doingTask.get(taskId);
        /**任务进度没有时，则返回0*/
        if (doingTask == null) {
            return 0;
        }
        return doingTask.getTargetProgress();
    }

    /**
     * 更新任务进度
     */
    public void updateTargetProgress(int taskId, int targetIndex, int targetCount) {
        DoingTask doingTask = this.doingTask.get(taskId);
        if (doingTask == null) {
            doingTask = new DoingTask(taskId, 0);
            this.doingTask.put(taskId, doingTask);
        }

        doingTask.addProgress(targetIndex);

        /**任务目标进度等于任务目标总数，则表示可以提交了*/
        if (doingTask.getTargetProgress() == targetCount) {
            updateToCanSubmit(taskId);
        }
    }

    /**重置任务进度*/
    public void revertTargetProgress(int taskId, int targetIndex) {
        DoingTask doingTask = this.doingTask.get(taskId);
        if (doingTask != null) {
            doingTask = new DoingTask(taskId, targetIndex);
            this.doingTask.put(taskId, doingTask);
        }

        doingTask.setTargetProgress(targetIndex);
    }

    /***
     * 获得所有已接任务的进度
     * 返回Map key: 任务ID，value： 任务对应的进度
     * @return
     */
    @JsonIgnore
    public Map<Integer, Integer> getAllAcceptedTaskProgress() {
        Map<Integer, Integer> progressMap = new HashMap<>(acceptedTasks.size());
        Set<Integer> taskIds = acceptedTasks.keySet();
        for (int taskId : taskIds) {
            int progress = getTargetProgress(taskId);
            progressMap.put(taskId, progress);
        }

        return progressMap;
    }

    /**获得所有已接任务列表*/
    @JsonIgnore
    public List<Integer> getAcceptedTaskList() {
        /**生成一个列表返回，不直接返回保存的集合，避免被误改*/
        List<Integer> list = new ArrayList<Integer>(acceptedTasks.size());
        list.addAll(acceptedTasks.keySet());
        return list;
    }

    /**添加战斗ID与任务ID的映射关系*/
    public void addRecord4FightAndTask(int fightId, int taskId) {
        DoingTask doingTask = this.doingTask.get(taskId);
        if (doingTask == null) {
            doingTask = new DoingTask(taskId, 0);
            this.doingTask.put(taskId, doingTask);
        }

        doingTask.setFightId(fightId);
    }

    /**根据战斗ID获得对应的任务ID(如果这场战斗由任务触发)*/
    public Integer getTaskIdByFightId(int fightId) {
        for (DoingTask doTask : doingTask.values()) {
            if (fightId == doTask.getFightId()) {
                return doTask.getTaskId();
            }
        }

        return null;
    }

    /**重置任务*/
    public void resetTask(int taskId) {
        deleteTask(taskId);

        /**重置时需要在已完成列表中删除*/
        finishedTasks.remove(taskId);
    }

    /**放弃任务*/
    public void giveUpTask(int taskId) {
        this.deleteTask(taskId);
    }

    /**删除任务*/
    public void deleteTask(int taskId) {
        removeAcceptedTask(taskId);
        removeDoingTask(taskId);
    }

    /**获得对应任务类型的放弃时间*/
    public long getGiveUpTime(TaskType taskType) {
        if (giveUpTimes != null && giveUpTimes.containsKey(taskType.getId())) {
            return giveUpTimes.get(taskType.getId());
        }

        return -1;
    }

    /**添加放弃任务的时间*/
    public void addGiveUpTime(TaskType taskType, long time) {
        if (giveUpTimes == null) {
            giveUpTimes = new HashMap<>();
        }
        giveUpTimes.put(taskType.getId(), time);
    }

    /**删除之前记录的放弃任务的时间*/
    public void removeGiveUpTime(TaskType taskType) {
        if (giveUpTimes != null) {
            giveUpTimes.remove(taskType.getId());
        }
    }

    /**添加任务对应的NPC Id*/
    public void addTaskNpcId(int taskId, int npcId) {
        DoingTask doingTask = this.doingTask.get(taskId);
        if (doingTask == null) {
            doingTask = new DoingTask(taskId, 0);
            this.doingTask.put(taskId, doingTask);
        }

        doingTask.setNpcId(npcId);
    }

    /**获得任务刷新出来 的对应的NPC id*/
    public int getTaskNpcId(int taskId) {
        DoingTask doingTask = this.doingTask.get(taskId);

        if (doingTask != null) {
            return doingTask.getNpcId();
        }

        return 0;
    }

    /**获得任务的接取时间*/
    public long getAcceptTime(int taskId) {
        DoingTask doingTask = this.doingTask.get(taskId);

        if (doingTask != null) {
            return doingTask.getCreateTime();
        }

        return 0;
    }

    public Map<Integer, Byte> getAcceptedTasks() {
        return acceptedTasks;
    }

    public Set<Integer> getFinishedTasks() {
        return finishedTasks;
    }

    @JsonIgnore
    public int getGatherTaskId() {
        return gatherTaskId;
    }

    public void setGatherTaskId(int gatherTaskId) {
        this.gatherTaskId = gatherTaskId;
    }

    @JsonIgnore
    public Set<Integer> getAcceptedTaskLists() {

        return acceptedTasks.keySet();
    }
}
